_script.js ➔ search   F
last analyzed

Complexity

Conditions 15

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
c 0
b 0
f 0
rs 2.9998
cc 15

How to fix   Complexity   

Complexity

Complex classes like _script.js ➔ search often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
$('i.glyphicon-refresh-animate').hide();
2
function updateRoutes(r) {
3
    _opts.routes.available = r.available;
0 ignored issues
show
Bug introduced by
The variable _opts seems to be never declared. If this is a global, consider adding a /** global: _opts */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
4
    _opts.routes.assigned = r.assigned;
5
    search('available');
6
    search('assigned');
7
}
8
9
$('#btn-new').click(function () {
10
    var $this = $(this);
11
    var route = $('#inp-route').val().trim();
12
    if (route != '') {
13
        $this.children('i.glyphicon-refresh-animate').show();
14
        $.post($this.attr('href'), {route: route}, function (r) {
15
            $('#inp-route').val('').focus();
16
            updateRoutes(r);
17
        }).always(function () {
18
            $this.children('i.glyphicon-refresh-animate').hide();
19
        });
20
    }
21
    return false;
22
});
23
24
$('.btn-assign').click(function () {
25
    var $this = $(this);
26
    var target = $this.data('target');
27
    var routes = $('select.list[data-target="' + target + '"]').val();
28
29
    if (routes && routes.length) {
30
        $this.children('i.glyphicon-refresh-animate').show();
31
        $.post($this.attr('href'), {routes: routes}, function (r) {
32
            updateRoutes(r);
33
        }).always(function () {
34
            $this.children('i.glyphicon-refresh-animate').hide();
35
        });
36
    }
37
    return false;
38
});
39
40
$('#btn-refresh').click(function () {
41
    var $icon = $(this).children('span.glyphicon');
42
    $icon.addClass('glyphicon-refresh-animate');
43
    $.post($(this).attr('href'), function (r) {
44
        updateRoutes(r);
45
    }).always(function () {
46
        $icon.removeClass('glyphicon-refresh-animate');
47
    });
48
    return false;
49
});
50
51
$('.search[data-target]').keyup(function () {
52
    search($(this).data('target'));
53
});
54
55
function search(target) {
56
    var $list = $('select.list[data-target="' + target + '"]');
57
    $list.html('');
58
    var q = $('.search[data-target="' + target + '"]').val();
59
    $.each(_opts.routes[target], function () {
0 ignored issues
show
Bug introduced by
The variable _opts seems to be never declared. If this is a global, consider adding a /** global: _opts */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
60
        var r = this;
61
        if (r.indexOf(q) >= 0) {
62
            $('<option>').text(r).val(r).appendTo($list);
63
        }
64
    });
65
}
66
67
// initial
68
search('available');
69
search('assigned');
70